// pages/sjzz/sjzz.js
const app = getApp();
const sendMessage = require('../../../pages/home/home')
const util = require('../../../utils/util')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    img:["https://dm-em03.obs.cn-north-4.myhuaweicloud.com/xunlian/xlimg/qx1.jpg","https://dm-em03.obs.cn-north-4.myhuaweicloud.com/xunlian/xlimg/qx2.jpg","https://dm-em03.obs.cn-north-4.myhuaweicloud.com/xunlian/xlimg/qx3.jpg","https://dm-em03.obs.cn-north-4.myhuaweicloud.com/xunlian/xlimg/qx4.jpg","https://dm-em03.obs.cn-north-4.myhuaweicloud.com/xunlian/xlimg/qx5.jpg","https://dm-em03.obs.cn-north-4.myhuaweicloud.com/xunlian/xlimg/qx6.jpg","https://dm-em03.obs.cn-north-4.myhuaweicloud.com/xunlian/xlimg/qx7.jpg","https://dm-em03.obs.cn-north-4.myhuaweicloud.com/xunlian/xlimg/qx8.jpg","https://dm-em03.obs.cn-north-4.myhuaweicloud.com/xunlian/xlimg/qx9.jpg","https://dm-em03.obs.cn-north-4.myhuaweicloud.com/xunlian/xlimg/qx10.jpg","https://dm-em03.obs.cn-north-4.myhuaweicloud.com/xunlian/xlimg/qx11.jpg","https://dm-em03.obs.cn-north-4.myhuaweicloud.com/xunlian/xlimg/qx12.jpg","https://dm-em03.obs.cn-north-4.myhuaweicloud.com/xunlian/xlimg/qx13.jpg"],
    list:[],
    imgList:[],
    second:180,
    times:0,
    num:0,
    timer:'',
    inter:'',
    grade:0,
    equipmentNumber:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.startInterval();
    this.startCountdown();
  },
  toImage(e){
    var id = e.currentTarget.dataset.id
    var num = this.data.num;
    if(num ==0){
      if (id==1){
        wx.showToast({
          title: '正确',
          icon: 'success',
          duration: 1000
        })
        num = num+1;
      } else {
        var grade = this.data.grade;
        grade = grade +1;
        this.setData({
          grade:grade
        })
        wx.showToast({
          title: '错误',
          icon: 'error',
          duration: 1000,
        })
        num = num+1;
      }
      this.setData({
        num:num
      })
    } else if (num==1){
      if (id==1){
        wx.showToast({
          title: '正确',
          icon: 'success',
          duration: 1000
        })
        num = num+1;
      } else {
        var grade = this.data.grade;
        grade = grade +1;
        this.setData({
          grade:grade
        })
        wx.showToast({
          title: '错误',
          icon: 'error',
          duration: 1000
        })
        num = num+1;
      }
      this.setData({
        num:num
      })
    } else if (num==2){
      if (id==1){
        wx.showToast({
          title: '正确',
          icon: 'success',
          duration: 1000
        })
        num = num+1;
      } else {
        var grade = this.data.grade;
        grade = grade +1;
        this.setData({
          grade:grade
        })
        wx.showToast({
          title: '错误',
          icon: 'error',
          duration: 1000
        })
        num = num+1;
      }
      this.setData({
        num:num
      })
    } else if (num==3){
      if (id==2){
        wx.showToast({
          title: '正确',
          icon: 'success',
          duration: 1000
        })
        num = num+1;
      } else {
        var grade = this.data.grade;
        grade = grade +1;
        this.setData({
          grade:grade
        })
        wx.showToast({
          title: '错误',
          icon: 'error',
          duration: 1000
        })
        num = num+1;
      }
      this.setData({
        num:num
      })
    } else if (num==4){
      if (id==3){
        wx.showToast({
          title: '正确',
          icon: 'success',
          duration: 1000
        })
        num = num+1;
      } else {
        var grade = this.data.grade;
        grade = grade +1;
        this.setData({
          grade:grade
        })
        wx.showToast({
          title: '错误',
          icon: 'error',
          duration: 1000
        })
        num = num+1;
      }
      this.setData({
        num:num
      })
    } else if (num==5){
      if (id==0){
        wx.showToast({
          title: '正确',
          icon: 'success',
          duration: 1000
        })
        num = num+1;
      } else {
        var grade = this.data.grade;
        grade = grade +1;
        this.setData({
          grade:grade
        })
        wx.showToast({
          title: '错误',
          icon: 'error',
          duration: 1000
        })
        num = num+1;
      }
      this.setData({
        num:num
      })
    } else if (num==6){
      if (id==0){
        wx.showToast({
          title: '正确',
          icon: 'success',
          duration: 1000
        })
        num = num+1;
      } else {
        var grade = this.data.grade;
        grade = grade +1;
        this.setData({
          grade:grade
        })
        wx.showToast({
          title: '错误',
          icon: 'error',
          duration: 1000
        })
        num = num+1;
      }
      this.setData({
        num:num
      })
    } else if (num==7){
      if (id==2){
        wx.showToast({
          title: '正确',
          icon: 'success',
          duration: 1000
        })
        num = num+1;
      } else {
        var grade = this.data.grade;
        grade = grade +1;
        this.setData({
          grade:grade
        })
        wx.showToast({
          title: '错误',
          icon: 'error',
          duration: 1000
        })
        num = num+1;
      }
      this.setData({
        num:num
      })
    } else if (num==8){
      if (id==1){
        wx.showToast({
          title: '正确',
          icon: 'success',
          duration: 1000
        })
        num = num+1;
      } else {
        var grade = this.data.grade;
        grade = grade +1;
        this.setData({
          grade:grade
        })
        wx.showToast({
          title: '错误',
          icon: 'error',
          duration: 1000
        })
        num = num+1;
      }
      this.setData({
        num:num
      })
    } else if (num==9){
      if (id==3){
        wx.showToast({
          title: '正确',
          icon: 'success',
          duration: 1000
        })
        num = num+1;
      } else {
        var grade = this.data.grade;
        grade = grade +1;
        this.setData({
          grade:grade
        })
        wx.showToast({
          title: '错误',
          icon: 'error',
          duration: 1000
        })
        num = num+1;
      }
      this.setData({
        num:num
      })
    } else if (num==10){
      if (id==1){
        wx.showToast({
          title: '正确',
          icon: 'success',
          duration: 1000
        })
        num = num+1;
      } else {
        var grade = this.data.grade;
        grade = grade +1;
        this.setData({
          grade:grade
        })
        wx.showToast({
          title: '错误',
          icon: 'error',
          duration: 1000
        })
        num = num+1;
      }
      this.setData({
        num:num
      })
    } else if (num==11){
      if (id==0){
        wx.showToast({
          title: '正确',
          icon: 'success',
          duration: 1000
        })
        num = num+1;
      } else {
        var grade = this.data.grade;
        grade = grade +1;
        this.setData({
          grade:grade
        })
        wx.showToast({
          title: '错误',
          icon: 'error', 
          duration: 1000
        })
        num = num+1;
      }
      this.setData({
        num:num
      })
    } else if (num==12){
      if (id==2){
        wx.showToast({
          title: '正确',
          icon: 'success',
          duration: 1000
        })
        num = num+1;
      } else {
        var grade = this.data.grade;
        grade = grade +1;
        this.setData({
          grade:grade
        })
        wx.showToast({
          title: '错误',
          icon: 'error',
          duration: 1000
        })
        num = num+1;
      }
      this.setData({
        num:num
      })
    }
    if (num == this.data.img.length){
      num =0;
      this.setData({
        num:num
      })
    }
    console.log(num);
    console.log(this.data.img.length);
    //this.onshow();
  },
  startCountdown() {
    var that = this;
    var second = that.data.second;
    var grade = that.data.grade;
    var equipmentNumber = app.nowEquipmentId;
    var score = 0;
    if (second == 0) {
      clearInterval(that.data.timer);
      clearInterval(that.data.inter);
      if (grade <=3){
        score = 5;
      } else if (grade <=6 && grade >3){
        score = 4;
      } else if (grade <=9 && grade >6){
        score = 3;
      } else {
        score = 2;
      }
      wx.navigateTo({
        url: '../../../pages/fraction/fraction?fraction='+score,
      })
    }
    that.data.timer = setTimeout(function () {
      that.setData({
        second: second - 1
      });
      that.startCountdown();
    } , 1000)
    var times = that.data.times;
    times++;
    that.setData({
      times:times,
      equipmentNumber:equipmentNumber
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {
    var openId= wx.getStorageSync('openid');
    clearInterval(this.data.timer);
    clearInterval(this.data.inter);
    this.setData({
      trainName:"曲线训练",
      openId:openId,
      times:180
    })
    util.saveRecord(this);
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
    var second = this.data.second;
    if (second ==0){
      return;
    }
    var openId= wx.getStorageSync('openid');
    clearInterval(this.data.timer);
    clearInterval(this.data.inter);
    this.setData({
      trainName:"曲线训练",
      openId:openId
    })
    util.saveRecord(this);
  },
  startInterval: function(){
    var that = this;
    that.data.inter= setInterval(function () {
      sendMessage.BLEDataSend("aa55a55a210100cc66c66c");
      console.log("定时任务开启");
    }, 10000)
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})